import fnmatch
import os
import glob

def rec_glob(path,filter):
    matches = []
    for root, dirnames, filenames in os.walk(path):
      for filename in fnmatch.filter(filenames, filter):
          matches.append(os.path.join(root, filename))
    return matches

def process_dtd(content):
    lines = content.splitlines()
    n_lines = len(lines)
    ret_dict = {}
    ret_names = []
    for i in range(n_lines):
        try:
            a,name,c = lines[i].split(' ',2)
        except:
            print "cannot process '%s'"%(lines[i])
        else:
            value = c[1:-2]
            name, value = name.strip(), value.strip()
            ret_names.append(name)
            ret_dict[name] = value

    return ret_dict, ret_names

def process_properties(content):
    lines = content.splitlines()
    n_lines = len(lines)
    ret_dict = {}
    ret_names = []
    for i in range(n_lines):
        try:
            name,value = lines[i].split('=',2)
        except:
            print "cannot process '%s'"%(lines[i])
        else:
            name, value = name.strip(), value.strip()
            ret_names.append(name)
            ret_dict[name] = value

    return ret_dict, ret_names

def unique(dict1,dict2):
    k1 = dict1.keys()
    k2 = dict2.keys()

    ret = {}

    for k in k2:
        if k in k1:
            if dict2[k] in ignore_these:
                ret[k] = dict2[k]
            elif dict2[k] == dict1[k] and (" " in dict2[k]):
                continue
            else:
                ret[k] = dict2[k]
    return ret

def write_dtd(fn,names,dict):
    lines = []
    dict_names = dict.keys()
    for name in names:
        if name in dict_names:
            l = "<!ENTITY %s \"%s\">\n"%(name,dict[name])
            lines.append(l)
    f = open(fn,"w")
    f.writelines(lines)
    f.close()

def write_properties(fn,names,dict):
    lines = []
    dict_names = dict.keys()
    for name in names:
        if name in dict_names:
            l = "%s=%s\n" %(name,dict[name])
            lines.append(l)
    f = open(fn,"w")
    f.writelines(lines)
    f.close()

#where the locale directory is
dir_locale = 'src/chrome/locale'

#These strings can be left unchanged...
ignore_these = ['Autofill Forms']

#first, get the en-US locale
fn_dtd = glob.glob(os.path.join(dir_locale,'en-US','*.dtd'))[0]
addon = os.path.split(fn_dtd)[1].split(".dtd")[0]

dtd_us, dtd_names = process_dtd(open(fn_dtd).read())
properties_us, properties_names = process_properties(open(os.path.join(dir_locale,'en-US',addon+'.properties')).read())

#these are the locales...
locales = os.listdir(dir_locale)

#processing each locale in turn
for d in locales:
    if d == "en-US":
        continue

    dd = os.path.join(dir_locale,d)
    if os.path.isdir(dd):
        print "doing",d
        fn_dtd = os.path.join(dd,addon+'.dtd')
        dict,names = process_dtd(open(fn_dtd).read())
        dtd = unique(dtd_us,dict)
        write_dtd(fn_dtd+"_aaa",dtd_names,dtd)

        fn_properties = os.path.join(dd,addon+'.properties')
        dict,names = process_properties(open(fn_properties).read())
        properties = unique(properties_us,dict)
        write_properties(fn_properties+"_aaa",properties_names,properties)

